package org.rsbot.gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import org.rsbot.util.FrameUtil;
import org.rsbot.util.Serializer;
import org.rsbot.util.UpdateUtil;

public class ThemesGui extends JFrame
{
  private JFrame gui;
  private JFrame curr;
  private static final Logger log = Logger.getLogger(ThemesGui.class.getName());
  private static boolean updated;
  private String name = "";
  private static Thread th = null;
  private JButton jButton1;
  private JComboBox jComboBox1;
  private JLabel jLabel1;

  public ThemesGui(JFrame paramJFrame)
  {
    this.gui = paramJFrame;
    initComponents();
    setDefaultCloseOperation(2);
    setLocationRelativeTo(getOwner());
    setTitle("Themes Selector");
    addOptions();
    setResizable(false);
    this.curr = this;
  }

  protected static boolean isProcessing() {
    return (th != null) && (th.isAlive());
  }

  protected static boolean isUpdated() {
    return updated;
  }

  private void addOptions() {
    this.jComboBox1.addItem("SubstanceAutumnLookAndFeel");
    this.jComboBox1.addItem("SubstanceNebulaLookAndFeel");
    this.jComboBox1.addItem("SubstanceNebulaBrickWallLookAndFeel");
    this.jComboBox1.addItem("SubstanceSaharaLookAndFeel");
    this.jComboBox1.addItem("SubstanceRavenLookAndFeel");
    this.jComboBox1.addItem("SubstanceOfficeSilver2007LookAndFeel");
    this.jComboBox1.addItem("SubstanceOfficeBlue2007LookAndFeel");
    this.jComboBox1.addItem("SubstanceModerateLookAndFeel");
    this.jComboBox1.addItem("SubstanceMistSilverLookAndFeel");
    this.jComboBox1.addItem("SubstanceMistAquaLookAndFeel");
    this.jComboBox1.addItem("SubstanceMagellanLookAndFeel");
    this.jComboBox1.addItem("SubstanceGraphiteLookAndFeel");
    this.jComboBox1.addItem("SubstanceGraphiteGlassLookAndFeel");
    this.jComboBox1.addItem("SubstanceGraphiteAquaLookAndFeel");
    this.jComboBox1.addItem("SubstanceGeminiLookAndFeel");
    this.jComboBox1.addItem("SubstanceEmeraldDuskLookAndFeel");
    this.jComboBox1.addItem("SubstanceDustLookAndFeel");
    this.jComboBox1.addItem("SubstanceDustCoffeeLookAndFeel");
    this.jComboBox1.addItem("SubstanceCremeLookAndFeel");
    this.jComboBox1.addItem("SubstanceCremeCoffeeLookAndFeel");
    this.jComboBox1.addItem("SubstanceChallengerDeepLookAndFeel");
    this.jComboBox1.addItem("SubstanceBusinessLookAndFeel");
    this.jComboBox1.addItem("SubstanceBusinessBlueSteelLookAndFeel");
    this.jComboBox1.addItem("SubstanceBusinessBlackSteelLookAndFeel");
  }

  private void initComponents()
  {
    this.jComboBox1 = new JComboBox();
    this.jButton1 = new JButton();
    this.jLabel1 = new JLabel();

    setDefaultCloseOperation(3);

    this.jComboBox1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent paramActionEvent) {
        ThemesGui.this.jComboBox1ActionPerformed(paramActionEvent);
      }
    });
    this.jButton1.setText("Save");
    this.jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent paramActionEvent) {
        ThemesGui.this.jButton1ActionPerformed(paramActionEvent);
      }
    });
    this.jLabel1.setText("Select a theme, and press on save to have it on further bot boots:");

    GroupLayout localGroupLayout = new GroupLayout(getContentPane());
    getContentPane().setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addGap(20, 20, 20).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addComponent(this.jLabel1, -1, -1, 32767).addComponent(this.jComboBox1, 0, 380, 32767))).addGroup(localGroupLayout.createSequentialGroup().addGap(124, 124, 124).addComponent(this.jButton1, -2, 142, -2))).addContainerGap(21, 32767)));

    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addGap(5, 5, 5).addComponent(this.jLabel1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jComboBox1, -2, -1, -2).addGap(18, 18, 18).addComponent(this.jButton1).addContainerGap(22, 32767)));

    pack();
  }

  private void jButton1ActionPerformed(ActionEvent paramActionEvent)
  {
    try {
      Serializer.serlialize(this.jComboBox1.getSelectedItem().toString(), new File("theme.pb"));
    } catch (Exception localException) {
      localException.printStackTrace();
    }
    dispose();
  }

  private void jComboBox1ActionPerformed(ActionEvent paramActionEvent)
  {
    this.name = this.jComboBox1.getSelectedItem().toString();

    SwingUtilities.invokeLater(new Runnable()
    {
      public void run() {
        if (!FrameUtil.setTheme(ThemesGui.this.gui, ThemesGui.this.name)) {
          JOptionPane.showMessageDialog(null, " Couldn't load the look and feel Substance,please make .\nsure that dependancies are in the lib folder int the jar's folder.");
          ThemesGui.this.dispose();
          int i = JOptionPane.showConfirmDialog(null, "Do you want to download the dependency files to the lib\nfolder in this jar's directory?\nyou have to restart bot right after", "Question", 0);

          if (i == 0) {
            File localFile = new File("lib");
            try {
              if (localFile.mkdir())
                System.out.println("Directory Created");
            }
            catch (Exception localException) {
              ThemesGui.log.info("couldnt create lib file, try again please");
              return;
            }
            ThemesGui.access$502(new Thread(new Runnable()
            {
              public void run()
              {
                ThemesGui.log.info("Downloading Themes Components, please wait ...");
                if ((UpdateUtil.download("http://f.nikkii.us/~pervyshuya/substance.jar", "lib/Substance.jar")) && 
                  (UpdateUtil.download("http://f.nikkii.us/~pervyshuya/trident.jar", "lib/trident.jar"))) {
                  ThemesGui.log.info("Downloaded needed dependancies! ,please restart bot and try selecting themes again!");
                  ThemesGui.access$602(true);
                }
              }
            }));
            ThemesGui.th.start();
          }
        }

        SwingUtilities.updateComponentTreeUI(ThemesGui.this.curr);
      }
    });
  }
}